<?
/**
 * User database object
 */
class User extends UserDbo {

/**
 * Constructor
 */
public function __construct() {
	parent::__construct();
	}

/**
 * Getters
 */
public function CreateDate() {
	return $this->getProperty('CreateDate');
	}
public function CreateUid() {
	return $this->getProperty('CreateUid');
	}
public function Description() {
	return $this->Name() . ' (' . $this->Login() . ')';
	}
public function Disabled() {
	return (!$this->Enabled());
	}
public function DisableDate() {
	return $this->getProperty('DisableDate');
	}
public function DisableUid() {
	return $this->getProperty('DisableUid');
	}
public function DisplayName() {
	return $this->getProperty('DisplayName');
	}
public function Enabled() {
	return ($this->getProperty('Enabled') == '1');
	}
public function FirstName() {
	return $this->getProperty('FirstName');
	}
public function Id() {
	return $this->getProperty('Id');
	}
public function isAdmin() {
	return ($this->getProperty('IsAdmin') == '1');
	}
public function IsEnabled() {
	return $this->Enabled();
	}
public function IsDisabled() {
	return $this->Disabled();
	}
public function LastName() {
	return $this->getProperty('LastName');
	}
public function Login() {
	return $this->getProperty('Login');
	}
public function Name() {
	return $this->DisplayName();
	}
public function RevDate() {
	return $this->getProperty('RevDate');
	}
public function RevUid() {
	return $this->getProperty('RevUid');
	}

/**
 * Overload the add method - set DisplayName to First/Last Name if not set.
 * @param bool $validate
 * @return bool true on success
 * @return string error message on failures.
 */
public function add($validate = true) {
	if (!$this->validateString($this->DisplayName())) {
		// Only do this if both first and last name is already set.
		$t1 = ($this->validateString($this->FirstName()));
		$t2 = ($this->validateString($this->LastName()));
		if ($t1 and $t2) {
			$dn = $this->FirstName() . ' ' . $this->LastName();
			$this->setProperty('DisplayName', $dn);
			}
		}
	$this->setProperty('CreateDate', 'now()');
	$this->setProperty('RevDate', 'now()');
	return parent::add($validate);
	}

/**
 * Can the currently populated item be deleted?
 * @return bool
 */
public function canDelete() {
	if (!$this->populated) { return false; }
	$id = $this->Id();
	if (!is_numeric($id) or $id < 3) { return false; }
	if ($this->isAdmin()) { return false; }
	return true;
	}

/**
 * Delete currently populated object, taking care of linked objects first.
 * @return bool true on success
 * @return string error message on failures.
 */
public function delete() {
	if (!$this->canDelete()) {
		return 'User cannot be deleted at this time.';
		}
	return parent::delete();
	}

/**
 * Obtain standardized object list.
 * @return array
 */
public function getList() {
	$this->searchReset();
	$this->setSearchSelect('Id');
	$this->setSearchSelect('DisplayName');
	$this->setSearchOrder('DisplayName');
	return $this->query();
	}

/**
 * Set currently populated user property flag to on (0) or off (1)
 * @param string $propName: Audit|Disabled|NeverExpires|PasschangeRequired|
 *                          PassChangeRestricted
 * @param int $flagValue: 0 (off/false) | 1 (on/true)
 * @return string error message on failures
 * @return bool true on success
 */
public function setFlag($propName = null, $flagValue = null) {
	// validate arguments
	if (!($this->populated === true)) { return 'user object not populated'; }
	switch($propName) {
		case 'Enabled':
		case 'IsAdmin':
		break;
		default:
			return 'Invalid flag property specified.';
		break;
		}
	switch($flagValue) {
		case '0':
		case '1':
		break;
		default:
			return 'Invalid flag value specified.';
		break;
		}

	// Set the new property and do the deed.
	$this->setProperty($propName, $flagValue);

	// Do the deed
	return ($this->update());
	}

/**
 * Overload the validation method.
 * @param bool skipId = skip validation of id property
 * @return bool true on success
 * @return string error message on failures.
 */
public function validate($skipId = false) {
	$rc = parent::validate($skipId);
	if (!($rc === true)) { return $rc; }

	// These properties are required.
	$props = array('Login', 'Password');
	foreach($props as &$prop) {
		if (!$this->validateString($this->getProperty($prop))) {
			return 'Must specify ' . $this->getPropertyDescription($prop);
			}
		}

	// These properties are required to be a 1 or 0
	$props = array('Enabled', 'IsAdmin');
	foreach($props as &$prop) {
		$val = $this->getProperty($prop);
		if (!($val == '0' or $val == '1')) {
			return 'Must specify a valid value for '
			. $this->getPropertyDescription($prop);
			}
		}

	// Password must be set...
	if (!($this->getProperty('Password') == '*')) {
		if (strlen($this->getProperty('Password')) != 32) {
			return 'Must set a valid password.';
			}
		}

	return true;
	}

}
?>
